<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form mb-4">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="名称" name="name">
                        <a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="试卷名称" name="paperName">
                        <a-input v-model:value="searchFormState.paperName" placeholder="请输入试卷名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-form-item label="考试开始时间" name="startTime">
                        <a-range-picker v-model:value="searchFormState.startTime" value-format="YYYY-MM-DD HH:mm:ss" show-time />
                    </a-form-item>
                </a-col>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="考试结束时间" name="endTime">
                            <a-range-picker v-model:value="searchFormState.endTime" show-time />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）" name="markState">
                            <a-input v-model:value="searchFormState.markState" placeholder="请输入阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="用户IDS" name="userIds">
                            <a-input v-model:value="searchFormState.userIds" placeholder="请输入用户IDS" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="机构IDS" name="orgIds">
                            <a-input v-model:value="searchFormState.orgIds" placeholder="请输入机构IDS" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="阅卷用户IDS" name="markUserIds">
                            <a-input v-model:value="searchFormState.markUserIds" placeholder="请输入阅卷用户IDS" />
                        </a-form-item>
                    </a-col>
                </template>
                <template v-if="advanced">
                    <a-col :span="6">
                        <a-form-item label="状态（0：删除；1：发布；2：暂停；）" name="status">
                            <a-input v-model:value="searchFormState.status" placeholder="请输入状态（0：删除；1：发布；2：暂停；）" />
                        </a-form-item>
                    </a-col>
                </template>
                <a-col :span="6">
                    <a-button type="primary" @click="table.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="() => searchFormRef.resetFields()">重置</a-button>
                    <a @click="toggleAdvanced" style="margin-left: 8px">
                        {{ advanced ? '收起' : '展开' }}
                        <component :is="advanced ? 'up-outlined' : 'down-outlined'"/>
                    </a>
                </a-col>
            </a-row>
        </a-form>
        <s-table
            ref="table"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.id"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
           <a-space>
                <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('exmExamAdd')">
                    <template #icon><plus-outlined /></template>
                    新增
                </a-button>
                <xn-batch-button
                    v-if="hasPerm('exmExamBatchDelete')"
                    buttonName="批量删除"
                       icon="DeleteOutlined"
                    :selectedRowKeys="selectedRowKeys"
                    @batchCallBack="deleteBatchExmExam"
                />
            </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('exmExamEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['exmExamEdit', 'exmExamDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteExmExam(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('exmExamDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
    <Form ref="formRef" @successful="table.refresh(true)" />
</template>

<script setup name="exam">
    import { message } from 'ant-design-vue'
    import Form from './form.vue'
    import exmExamApi from '@/api/biz/exmExamApi'
    let searchFormState = reactive({})
    const searchFormRef = ref()
    const table = ref()
    const formRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    // 查询区域显示更多控制
    const advanced = ref(false)
    const toggleAdvanced = () => {
        advanced.value = !advanced.value
    }
    const columns = [
        {
            title: '名称',
            dataIndex: 'name'
        },
        {
            title: '试卷名称',
            dataIndex: 'paperName'
        },
        {
            title: '组卷方式（1：人工组卷；2：随机组卷）数据字典：PAPER_GEN_TYPE',
            dataIndex: 'genType'
        },
        {
            title: '阅卷方式（1：客观题；2：主观题；）数据字典：PAPER_MARK_TYPE',
            dataIndex: 'markType'
        },
        {
            title: '展示方式（1：整卷展示；3：单题展示；）数据字典：PAPER_SHOW_TYPE',
            dataIndex: 'showType'
        },
        {
            title: '登录方式（1：正常登录；2：免登录；）数据字典：LOGIN_TYPE',
            dataIndex: 'loginType'
        },
        {
            title: '考试开始时间',
            dataIndex: 'startTime'
        },
        {
            title: '考试结束时间',
            dataIndex: 'endTime'
        },
        {
            title: '限制分钟（考试开始时间由用户第一次打开试卷时计时）',
            dataIndex: 'limitMinute'
        },
        {
            title: '阅卷开始时间',
            dataIndex: 'markStartTime'
        },
        {
            title: '阅卷结束时间',
            dataIndex: 'markEndTime'
        },
        {
            title: '阅卷状态（1：未阅卷；2：阅卷中；3：已阅卷；）',
            dataIndex: 'markState'
        },
        {
            title: '成绩查询状态（1：考试结束后；2：不公布；3：交卷后）',
            dataIndex: 'scoreState'
        },
        {
            title: '排名状态（1：公布；2：不公布）',
            dataIndex: 'rankState'
        },
        {
            title: '匿名阅卷状态（1：是；2：否）',
            dataIndex: 'anonState'
        },
        {
            title: '及格分数',
            dataIndex: 'passScore'
        },
        {
            title: '总分数',
            dataIndex: 'totalScore'
        },
        {
            title: '防作弊（1：试题乱序；2：选项乱序；3：禁止考试中切屏；4：禁止浏览器调试）',
            dataIndex: 'sxes'
        },
        {
            title: '用户IDS',
            dataIndex: 'userIds'
        },
        {
            title: '机构IDS',
            dataIndex: 'orgIds'
        },
        {
            title: '阅卷用户IDS',
            dataIndex: 'markUserIds'
        },
        {
            title: '重考次数',
            dataIndex: 'retakeNum'
        },
        {
            title: '状态（0：删除；1：发布；2：暂停；）',
            dataIndex: 'status'
        },
    ]
    // 操作栏通过权限判断是否显示
    if (hasPerm(['exmExamEdit', 'exmExamDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }
    let selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        alert: {
            show: false,
            clear: () => {
                selectedRowKeys = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = JSON.parse(JSON.stringify(searchFormState))
        // startTime范围查询条件重载
        if (searchFormParam.startTime) {
            searchFormParam.startStartTime = searchFormParam.startTime[0]
            searchFormParam.endStartTime = searchFormParam.startTime[1]
            delete searchFormParam.startTime
        }
        // endTime范围查询条件重载
        if (searchFormParam.endTime) {
            searchFormParam.startEndTime = searchFormParam.endTime[0]
            searchFormParam.endEndTime = searchFormParam.endTime[1]
            delete searchFormParam.endTime
        }
        return exmExamApi.exmExamPage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
    // 删除
    const deleteExmExam = (record) => {
        let params = [
            {
                id: record.id
            }
        ]
        exmExamApi.exmExamDelete(params).then(() => {
            table.value.refresh(true)
        })
    }
    // 批量删除
    const deleteBatchExmExam = () => {
        if (selectedRowKeys.value.length < 1) {
            message.warning('请选择一条或多条数据')
            return false
        }
        const params = selectedRowKeys.value.map((m) => {
            return {
                id: m
            }
        })
        exmExamApi.exmExamDelete(params).then(() => {
            table.value.clearRefreshSelected()
        })
    }
</script>
